home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™ 1987-1994 / MacHack™ '93 / Papers '93 / Macintosh as Internet Server ƒ / inetd / talkd / talkd.cp < prev    next >
Encoding:
Text File  |  1993-03-19  |  2.2 KB  |  110 lines  |  [TEXT/MPS ]

  1. //---------------------------------------------------------------------
  2. //
  3. //    Copyright © 1992 David Peterson.
  4. //    All rights reserved.
  5. //
  6. //    Permission to use, copy, modify, and distribute this software for
  7. //    any purpose and without fee is hereby granted, provided that the
  8. //    above copyright notice appear in all copies and that both that
  9. //    copyright notice and this permission notice appear in supporting
  10. //    documentation.
  11. //
  12. //---------------------------------------------------------------------
  13.  
  14. #include "TalkD.h"
  15.  
  16. #include "U_ctl.h"
  17. #define VERSION "with old talk protocol (otalk)"
  18.  
  19. #include <myUtils.h>
  20. #include <UDP.h>
  21. #include <DNR.h>
  22.  
  23. #include <StdIO.h>
  24. #include <String.h>
  25. #include <Sysequ.h>
  26. #include <Memory.h>
  27. #include <Packages.h>
  28.  
  29. void main();
  30.  
  31. void
  32. main()
  33. {
  34.     InitUDaemonApp(*((long*) DefltStack));
  35.  
  36.     TalkD* aDaemon = new TalkD;
  37.     if (aDaemon) {
  38.         aDaemon->Initialize();
  39.         aDaemon->Run();
  40.  
  41.         delete aDaemon;
  42.     }
  43. }
  44.  
  45. void 
  46. TalkD::InstallAEHandlers()
  47. {
  48.     Inherited::InstallAEHandlers();
  49.  
  50.     OSErr    theErr;
  51.  
  52.     theErr = AEInstallEventHandler('INET', 'USTR', 
  53.                 (EventHandlerProcPtr) &AEStreamHandler, (long) this, false);
  54.  
  55.     if (theErr != noErr)
  56.         ExitToShell();
  57. }
  58.  
  59. pascal OSErr 
  60. AEStreamHandler(AppleEvent* messagein, AppleEvent* /*reply*/, long refIn)
  61. {
  62.     AEDesc        streamDesc;
  63.     OSErr        theErr;
  64.     StreamPtr    stream;
  65.  
  66.     if ((theErr = AEGetParamDesc(messagein, 'STRM', typeLongInteger, &streamDesc)) == noErr) {
  67.         HLock(streamDesc.dataHandle);
  68.         stream = *((StreamPtr*) *streamDesc.dataHandle);
  69.         HUnlock(streamDesc.dataHandle);
  70.         
  71.         theErr = AEDisposeDesc(&streamDesc);
  72.     }
  73.     
  74.     if (theErr == noErr)
  75.         ((TalkD*) refIn)->HandleNewStream(stream);
  76.  
  77.     return theErr;
  78. }
  79.  
  80. void
  81. TalkD::HandleNewStream(StreamPtr stream)
  82. {
  83.     CTL_MSG        msg;
  84.     short        len;
  85.     char        remote[32];
  86.     char        string[255];
  87.     
  88.     unsigned long    time;
  89.     Str63            datestr;
  90.     Str63            timestr;
  91.  
  92.     len = sizeof(msg);
  93.     udpRead(stream, &msg, &len);
  94.  
  95.     GetDateTime(&time);
  96.     IUDateString(time, shortDate, datestr);
  97.     IUTimeString(time, false, timestr);
  98.     
  99.     dnrAddrToName(msg.ctl_addr.sin_addr.s_addr, remote, nil, nil);
  100.     
  101.     sprintf(string, "Talk Daemon @ %P, %P:\n\n%s at %s wants to talk to %s\n%s",
  102.                     timestr, datestr, msg.l_name, remote, msg.r_name, VERSION);
  103.     
  104.     this->PostNotification(string);
  105.     
  106.     udpRelease(stream);
  107.     
  108.     this->DoQuit();
  109. }
  110.